<template>
  <div class="container" v-if="!showDetail">
    <div class="header">
      <div class="title">Banner列表</div>
    </div>
    <el-table stripe v-loading="loading" :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="id" width="100"> </el-table-column>
      <el-table-column label="图片" width="200">
        <el-image
          slot-scope="scope"
          :preview-src-list="imgSrcList"
          :src="scope.row.img"
          style="max-height: 50px; max-width: 120px"
        />
      </el-table-column>
      <el-table-column prop="name" width="150" label="名称"> </el-table-column>
      <el-table-column prop="title" width="150" label="标题"> </el-table-column>
      <el-table-column min-width="200" :show-overflow-tooltip="true" prop="description" label="描述"> </el-table-column>
      <el-table-column fixed="right" width="150" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleDetail(scope.row)" size="mini" type="primary" plain>查看</el-button>
          <el-button
            @click="deleteDetail(scope.row)"
            
            v-permission="{ permission: '删除Banner', type: 'disabled' }"
            size="mini"
            type="danger"
            plain
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        :page-count="pageCount"
        @current-change="handleChangePage"
        :background="true"
        :total="total"
        layout="prev, pager, next, jumper"
      ></el-pagination>
    </div>
  </div>
  <banner-detail :banner-id="bannerDetailId" @go-back="onDetailClose" v-else></banner-detail>
</template>

<script>
import { Banner } from '@/model/banner'
import bannerDetail from '@/view/banner/banner-detail.vue';
export default {
  data() {
    return {
      tableData: [],
      showDetail: false,
      imgSrcList: [], // 用于大图预览
      bannerDetailId: -1,
      currentPage: 1,
      pageCount: 5,
      total: 0
    }
  },
  components: { bannerDetail },
  created() {
    this.loading = true
    this.getBannerList()
    this.loading = false

  },
  methods: {
    async getBannerList() {
      const page = this.currentPage - 1
      const count = this.pageCount
      const res = await Banner.getBanners(page, count)
      this.total = res.total
      this.pageCount = this.calPageTotal(res.total, 1)
      this.tableData = res.items
      this.initImgSrcList()
    },
    async getBannersAfterDelete() {
      const page = this.currentPage - 1
      const res = await Banner.getBanners(page, 1)
      this.pageCount = this.calPageTotal(this.total, 1)
      this.tableData = res.items
    },
    calPageTotal(total, perPageCount) {
      const num = Math.ceil(total / perPageCount)
      return num
    },
    initImgSrcList() {
      this.tableData.forEach(item => {
        if (!item.img) {
          return
        }
        this.imgSrcList.push(item.img)
      })
    },
    handleChangePage(val) {
      this.currentPage = val
      this.getBannerList()
      console.log(val);
    },
    handleDetail(val) {
      this.showDetail = true
      this.bannerDetailId = val.id
    },
    deleteDetail(val) {
      this.$confirm('此操作将永久删除该Banner，是否继续？', '提示', {
        type: 'warning'
      }).then(async () => {
        await Banner.deleteBanner(val.id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        // 需要同步执行
        this.getBannersAfterDelete()
      })
    },
    onDetailClose() {
      this.showDetail = false

    }
  }

}
</script>

<style lang="scss" scoped>
.container {
  padding: 30px 30px 0 30px;

  .title {
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 20px;
    color: $theme;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin: 20px;
}
</style>